<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageCollectService;
use plugin\ycMassage\app\model\MassageOrdersSub;
use plugin\ycMassage\app\model\MassageService;
use plugin\ycMassage\app\model\MassageServiceComment;
use plugin\ycMassage\app\model\MassageServiceImage;
use plugin\ycMassage\app\model\MassageUserTechnicianService;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;

class ServiceController extends BaseController
{
    protected $noNeedLogin = ["list", "details"];
    public function list(Request $request)
    {
        $saas_appid = $request->appid;
        $uid = $request->uid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $limit = $request->get('limit', 10);
        $page = $request->get('page');
        $where = [];
        $where[] = ['s.saas_appid', '=', $saas_appid];
        $where[] = ['s.state', '=', State::YES['value']];
        $where[] = ['s.examine', '=', Examine::YES['value']];
        $keywords = $request->get('keywords', '');
        if ($keywords) {
            $where[] = ['s.title', 'like', "%{$keywords}%"];
        }
        $field = [
            's.*', 's.sales_num + s.virtually_sales_num as sales_num'
        ];
        $model = MassageService::alias('s')->where($where);
        $technician_id = $request->get('technician_id');
        if ($technician_id) {
            $field[] = 'uts.price as uts_price';
            $model->join('massage_user_technician_service uts', "uts.service_id=s.id AND uts.state= " . State::YES['value'] . " AND uts.uid={$technician_id}");
        }
        $model->field($field);
        $order = 's.sort asc,s.id desc';
        $sort = $request->get('sort');
        $sc = $request->get('order');
        switch ($sort . '_' . $sc) {
            case 'sale_desc':
                $order = '(s.sales_num + s.virtually_sales_num) desc,s.id desc';
                break;
            case 'sale_asc':
                $order = '(s.sales_num + s.virtually_sales_num) asc,s.id desc';
                break;
            case 'price_desc':
                $order = 's.price desc,s.id desc';
                break;
            case 'price_asc':
                $order = 's.price asc,s.id desc';
                break;
            case 'comment_desc':
            case 'comment_asc':
                $order = 's.comment_grade desc,s.id desc';
                break;
        }
        if ($page) {
            $listObj = $model->orderRaw($order)->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        } else {
            $listObj = $model->limit($limit)->orderRaw($order)->select();
        }
        $list = $listObj->each(function ($item) use ($uid,$technician_id) {
            $item->num = 0;
            if ($item->uts_price > 0) {
                $item->price = $item->uts_price;
            }
            $item->is_collect = 0;
            if ($uid) {
                $item->is_collect = MassageCollectService::where(['service_id' => $item->id, 'uid' => $uid])->count();
            }
            $where=['service_id'=>$item->id];
            if($technician_id){
                $where['technician_id']=$technician_id;
            }
            $orderSub=MassageOrdersSub::where($where)->order('id desc')->find();
            if($orderSub&&time()-strtotime($orderSub->create_time)<3600*12){
                $item->last_time=$orderSub->create_time;
            }
        });
        return $this->successRes($list);
    }
    public function details(Request $request)
    {
        $saas_appid = $request->appid;
        $uid = $request->uid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $id = $request->get('id');
        $MassageService = MassageService::where(['id' => $id, 'saas_appid' => $saas_appid])->find();
        if (!$MassageService) {
            return $this->fail('服务不存在');
        }
        $thumbsData = [];
        if ($MassageService->video) {
            $thumbsData[] = [
                'url' => $MassageService->video,
                'type' => 'video'
            ];
        }
        $MassageService->content = [];
        $MassageService->taboo = [];
        $MassageService->order = [];
        $thumbs = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'thumbs', 'saas_appid' => $MassageService->saas_appid])->find();
        if ($thumbs) {
            foreach ($thumbs->images as $key => $url) {
                $thumbsData[] = [
                    'url' => $url,
                    'type' => 'image'
                ];
            }
        }
        $MassageService->thumbs = $thumbsData;
        $content = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'content', 'saas_appid' => $MassageService->saas_appid])->find();
        if ($content) {
            $MassageService->content = $content->images;
        }
        $taboo = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'taboo', 'saas_appid' => $MassageService->saas_appid])->find();
        if ($taboo) {
            $MassageService->taboo = $taboo->images;
        }
        $order = MassageServiceImage::where(['service_id' => $MassageService->id, 'action' => 'order', 'saas_appid' => $MassageService->saas_appid])->find();
        if ($order) {
            $MassageService->order = $order->images;
        }
        $MassageService->sales_num = $MassageService->sales_num + $MassageService->virtually_sales_num;
        $MassageService->max_price = MassageUserTechnicianService::where(['service_id' => $MassageService->id, 'saas_appid' => $MassageService->saas_appid])->max('price');
        $MassageService->is_collect = 0;
        if ($uid) {
            $MassageService->is_collect = MassageCollectService::where(['service_id' => $MassageService->id, 'uid' => $uid])->count();
        }

        $where = [
            'comment.saas_appid' => $saas_appid,
            'comment.service_id' => $MassageService->id,
            'comment.show' => State::YES['value'],
            'comment.examine' => Examine::YES['value']
        ];
        $comment = MassageServiceComment::alias('comment')->where($where)
            ->join('massage_user u', 'u.id=comment.uid')
            ->field('comment.*,u.nickname,u.headimg')
            ->order('comment.grade desc,comment.id desc')
            ->limit(5)
            ->select()->each(function ($item) use ($saas_appid) {
                if ($item->anony) {
                    $item->nickname = '匿名用户';
                    $item->headimg = UploadService::url(getHpConfig('default_avatar', $saas_appid, 'user'));
                } else {
                    $item->nickname = base64_decode($item->nickname);
                    if (strpos($item->headimg, 'http') !== 0) {
                        $item->headimg = UploadService::url($item->headimg);
                    }
                }
            });
        $MassageService->comments = $comment->toArray();
        $MassageService->comments_count=MassageServiceComment::alias('comment')->where($where)->count();
        return $this->successRes($MassageService);
    }
}
